package com.baozhun.mall.common.widget

import android.content.Context
import android.content.res.TypedArray
import android.util.AttributeSet
import android.view.View
import android.widget.LinearLayout
import androidx.databinding.DataBindingUtil
import com.baozhun.mall.common.R
import com.baozhun.mall.common.databinding.ViewItemSellingPointBinding

/**
 * @Title：卖点自定义view
 * @Description：
 * @Author：xuefeng.zhu
 * @Since：2023/12/19
 * @Version：1.0.0
 */
class SellingPointView(context: Context, private var attrs: AttributeSet? = null) : LinearLayout(context, attrs, 0) {
    private var mSellingPoints: Array<String>? = arrayOf()

    init {
        orientation = HORIZONTAL
        val typedArray: TypedArray = context.obtainStyledAttributes(attrs, R.styleable.SellingPointsView, 0, 0)
        var sellingPointsArrayId = typedArray.getResourceId(R.styleable.SellingPointsView_sellingPoints, 0)
        if (sellingPointsArrayId != 0) {
            mSellingPoints = resources.getStringArray(sellingPointsArrayId)
        }
        typedArray.recycle()
    }

    /**
     * 设置卖点
     */
    fun setSellingPoints(sellingPoints: Array<String>?) {
        mSellingPoints = sellingPoints
        if (mSellingPoints.isNullOrEmpty()) return
        removeAllViews()
        for (item in mSellingPoints!!) {
            var viewItemSellingPointBinding = DataBindingUtil.bind<ViewItemSellingPointBinding>(View.inflate(context, R.layout.view_item_selling_point, null))
            viewItemSellingPointBinding?.itemValues = item
            addView(viewItemSellingPointBinding?.root)
        }
    }

}